﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Configuration;

/// <summary>
/// Mailer 的摘要描述
/// </summary>
public class Mailer
{
    public Mailer()
	{
		//
		// TODO: 在此加入建構函式的程式碼
		//
	}
    public Mailer(string host, string port, string fromAddress, string toAddress, string subject, string display ,string body)
    {
        _smtpServer = host;
        _port = port;
        _fromMailAddress = fromAddress;
        _mailTo = toAddress;
        _DisplayName = display;
        _subject = subject;
        _body = body;
    }

    /// <summary>
    /// 發送Email作業
    /// </summary>
    /// <param name="strSbj"></param>
    /// <param name="strMsg"></param>
    public void send(string strSbj, string strMsg)
    {
        try
        {
            if (_mailTo.Replace(";", "") != "")
            {
                SmtpClient smtpClient = new SmtpClient();
                MailMessage message = new MailMessage();
                MailAddress fromAddress = new MailAddress(_fromMailAddress, _DisplayName);
                smtpClient.Host = _smtpServer;
                smtpClient.Port = Convert.ToInt16(_port);
                message.From = fromAddress;
                foreach (string mailTo in _mailTo.Split(';'))
                {
                    if (mailTo != "")
                        message.Bcc.Add(mailTo);
                }
                message.Subject = strSbj;
                message.SubjectEncoding = System.Text.Encoding.UTF8;
                message.IsBodyHtml = true;
                message.Body = strMsg;
                message.BodyEncoding = System.Text.Encoding.UTF8;
                smtpClient.Send(message);
            }
        }
        catch (Exception exc)
        {
            throw new Exception("Send Mail Error-->" + exc.Message);
        }            
    }

    private string _smtpServer;
    public string smtpServer
    {
        get
        {
            return _smtpServer;
        }
        set
        {
            _smtpServer = value;
        }
    }
    private string _port;
    public string port
    {
        get
        {
            return _port;
        }
        set
        {
            _port = value;
        }
    }
    private string _fromMailAddress;
    public string fromMailAddress
    {
        get
        {
            return _fromMailAddress;
        }
        set
        {
            _fromMailAddress = value;
        }
    }
    private string _mailTo;
    public string mailTo
    {
        get
        {
            return _mailTo;
        }
        set
        {
            _mailTo = value;
        }
    }
    private string _DisplayName;
    public string DisplayName
    {
        get
        {
            return _DisplayName;
        }
        set
        {
            _DisplayName = value;
        }
    }
    private string _subject;
    public string subject
    {
        get
        {
            return _subject;
        }
        set
        {
            _subject = value;
        }
    }
    private string _body;
    public string body
    {
        get
        {
            return _body;
        }
        set
        {
            _body = value;
        }
    }
}
